package com.etc.controller;

import com.etc.common.Const;
import com.etc.common.ServerResponse;
import com.etc.entity.Joke;
import com.etc.entity.User;
import com.etc.service.JokeService;
import com.etc.vo.JokeVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * 
 * @author zhp
 * @descripe 笑话类别的control层
 * @date 2018/8/4 20:05
 */
@Controller
@RequestMapping("/joke")
public class JokeController {
	@Autowired
	private JokeService jokeService;

	@RequestMapping("insert.do")
	@ResponseBody
	public ServerResponse<PageInfo> insertJoke(Joke joke,HttpSession session,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10")int pageSize){
		User user = (User)session.getAttribute("USER_NAME");
		joke.setjUid(user.getUid());
		//前端ajax判断返回结果
		return jokeService.insertJoke(joke,pageNum,pageSize);
		
	}
	/**
	 * 查找：通过 类型 查询
	 */
	@RequestMapping("select.do")
	public String selectByJcategory(Model model,HttpSession session,@RequestParam(value = "jcategory",defaultValue = "0") Integer jcategory, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
									@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
		User user = (User)session.getAttribute("USER_NAME");

		//todo 要加一个requestmapping 来验证是否是用户个人空间查看

		//前端ajax判断返回结果
		System.out.println(jcategory);
		ServerResponse<PageInfo> res = jokeService.selectByJcategory(0,jcategory,pageNum,pageSize);
		System.out.println(res.getData());
		model.addAttribute("jcategory",jcategory);
		model.addAttribute("res",res.getData());
		return "laugh";
	}
	/**
	 * @param jid 文章的ID
	 * @param attitude : 从前台传来的标志 判断是点赞:1 还是踩灭:0
	 * @param session
	 * @return
	 */
	@RequestMapping("attitude")
	@ResponseBody
	public ServerResponse chooseAttitude(@RequestParam("jid") Integer jid,@RequestParam("attitude") Integer attitude,HttpSession session,Model model){
		//TODO 应在开始时设置拦截器 判断是否登录
		User user = (User)session.getAttribute("USER_NAME");
		ServerResponse	response = jokeService.judgeAndHandleAttitude(jid,attitude,2);
		//如果成功 code = 0 则前台进行按钮的状态更改
		ServerResponse<JokeVo> res;
		if (response.isSuccess()){
			res = jokeService.selectOneByjid(jid);
			return res;
		}
		res = ServerResponse.createByError();
		return res;
	}

	/**
	 *
	 * @Description: 管理员操作 更改审核状态 -1 ==> 审核不通过; 1 ==> 审核通过
	 *
	 * @auther: 臧浩鹏
	 * @date: 12:00 2018/8/11
	 * @param: [jid, status]
	 * @return: com.etc.common.ServerResponse
	 *
	 */
	@RequestMapping("/admin/changeJstatus")
    @ResponseBody
	public ServerResponse changeJstatus(@RequestParam("jid") Integer jid, @RequestParam("status") Integer status){
        ServerResponse response = jokeService.checkJstatus(jid, status);
        return response;
    }


    /**
     *
     * @Description: 管理员操作 根据审核状态查找 joke
     *
     * @auther: 臧浩鹏
     * @date: 11:59 2018/8/11
     * @param: []
     * @return: java.lang.String
     *
     */
    @RequestMapping("/admin/selectByStatus")
	public String selectByStatus(Model model,HttpSession session,@RequestParam(value = "status",defaultValue = "0") Integer status,@RequestParam(value = "jcategory",defaultValue = "0") Integer jcategory, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
								 @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){

		ServerResponse<PageInfo> res = jokeService.selectByStatus(status,pageNum,pageSize);

		model.addAttribute("status",status);
		model.addAttribute("res",res.getData());

    	return "admin_jokeStatus";
	}

	@RequestMapping("/admin/selectUnPass")
	public String selectUnPass(Model model,HttpSession session,@RequestParam(value = "jcategory",defaultValue = "0") Integer jcategory, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
							   @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
		ServerResponse<PageInfo> res = jokeService.selectByStatus(Integer.parseInt(Const.Status.UNPASS),pageNum,pageSize);
		model.addAttribute("res",res.getData());
		return "admin_jokeDel";
	}

	@RequestMapping(value = "/admin/delUnPass",method = RequestMethod.POST)
    @ResponseBody
	public ServerResponse delUnPass(@RequestParam("jid") Integer jid){
		if (jid==null){
			return ServerResponse.createByErrorMessage("请输入id！");
		}
		return jokeService.deleteJoke(jid);
	}

	@RequestMapping(value = "/admin/delUnPassAll",method = RequestMethod.POST)
    @ResponseBody
	public ServerResponse delUnPassAll(@RequestBody String[] ids) throws IOException {
        if (ids==null||ids.length<1){
			return ServerResponse.createByErrorMessage("请选择要删除的joke！");
		}
		ServerResponse response = jokeService.deleteJokeByBatch(ids);
		return response;
	}
}
